package com.example.demo.service.serviceImpl;

import com.example.demo.dao.DashboardDao;
import com.example.demo.entity.Dashboard;
import com.example.demo.service.DashboardService;
import com.example.demo.util.Result;
import com.example.demo.util.ResultSubmit;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    private DashboardDao dao;

    SimpleDateFormat format =
            new SimpleDateFormat("yyyy-MM");

    @Override
    public Result selectRoomNumber() {
        return ResultSubmit.success(dao.selectRoomNumber());
    }

    @Override
    public Result selectLiveNumber() {
        return ResultSubmit.success(dao.selectLiveNumber());
    }

    @Override
    public Result selectRevenue(String month) {
        return ResultSubmit.success(dao.selectRevenue(month+"-01 00:00:00"));
    }

    @Override
    public Result selectRevenueByMonth(String month) {
        List<Dashboard> dashboards = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            Dashboard dashboard = new Dashboard();
            dashboard.setMonth(month);
            dashboard.setRevenue(dao.selectRevenue(month+"-01 00:00:00"));
            dashboards.add(dashboard);
            Date date = null;
            try {
                date = format.parse(month+"-01 00:00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            month = format.format(calendar.getTime());
        }
        return ResultSubmit.success(dashboards);
    }
}